<?php 
    /**
     *  This file outputs cart
     * 
     *  Его можно вызывать через AJAX, при этом передавая такие данные:
     *  id_product_delete - Продукт с таким ID будет удален из корзины
     *  id_product_change_quantity - У продукта с таким ID будет изменено количество на product_change_quantity
     *  product_change_quantity - Новое количесто для продукта, заданного в id_product_change_quantity
     * 
     */     
    require_once('../../includes/spip_launcher.php');
    include_spip('functions/pdk_common');


    $cart = pdk_load_class('Alohacart','classes');
    $product_mdl = pdk_load_class('ProductModel','classes');
    $currency_mdl = pdk_load_class('CurrencyModel','classes');

// If we have product to delete - delete it
    $id_product_delete = _request('id_product_delete'); //$id_product_delete = intval(_request('id_product_delete'));
    if ($id_product_delete)
        $cart->RemoveProduct($id_product_delete);
    
// If we have product with changed quantity - change it
    $id_product_change_quantity = intval(_request('id_product_change_quantity'));
    $product_change_quantity = intval(_request('product_change_quantity'));
    $not_enough_products_script = "";
    if ($id_product_change_quantity) {
        $product = $product_mdl->select_by_id($id_product_change_quantity);    
        if ($product_change_quantity > $product['quantity'] && lire_config('alohacart/config_consider_quantity')) {
            $not_enough_products_script = "<script>show_dialog('not_enough_products');</script>";
            $cart->UpdateProduct($id_product_change_quantity, $product['quantity']);
        } else
            $cart->UpdateProduct($id_product_change_quantity, $product_change_quantity);
    }
    
// Define some data. This code is similar to code in charger function
    $_REQUEST['products_ids'] = $cart->GetProductsIDs();
    $_REQUEST['products'] = $cart->GetProducts();

// Calculate prices for products according to quantities
    $_REQUEST['products_prices'] = $cart->GetProductsTotals();
    
// Order total price
    $_REQUEST['order_total_price'] = $cart->CountPrice();
    $_REQUEST['order_total_price_min'] = lire_config('alohacart/order_total_price_min');
    $_REQUEST['order_min_quantity'] = lire_config('alohacart/order_min_quantity');

// Shipping cost
    if ($id_shipping = intval($_REQUEST['id_shipping'])) {
        $extension_mdl = pdk_load_class('AlohacartExtensionModel','classes');
        $shipping_extension = $extension_mdl->GetExtensionClassByID($id_shipping);
        $shipping_cost = $currency_mdl->ConvertPriceToCurrentCurrency($shipping_extension->GetCost());
        if ($shipping_cost) {
            $_REQUEST['shipping_cost'] = $shipping_cost;
            $_REQUEST['order_total_price'] += $shipping_cost;
            if(strstr($_REQUEST['order_total_price'],'.')!==FALSE) { // if $price is float
                $_REQUEST['order_total_price'] = number_format($_REQUEST['order_total_price'],2,'.',''); // make two digits after point
            }
        } else {
            $_REQUEST['shipping_cost'] = '';
            // If no shipping cost - don't output order total price
            $_REQUEST['order_total_price'] = '';
        }

    } else {
        // If no shipping - don't output order total price
        $_REQUEST['order_total_price'] = '';
    }

	// Output cart's template:
	//print_r($_SESSION);
	//echo "product: $product id $id_product_change_quantity";
    echo recuperer_fond('formulaires/alohacart_cart_order/cart', $_REQUEST).$not_enough_products_script;
?>
